package experiment1;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * @author mazouri
 * @create 2021-09-23 10:50
 */
public class Goldbach {


    /**
     * @param args
     * @throws IOException
     */
    public static int cnt;

    public static void main(String[] args) throws IOException {
        Writer file = new FileWriter("diagram.csv");
        file.write("n,times\n");
        String line;
        for (int n = 200; n <= 50000; n += 50) {
            cnt = 0;
            if (passGoldConj(n)) {
                line = Integer.toString(n) + "," + Integer.toString(cnt) + "\n";
                file.write(line);
            } else {
                System.out.println("Goldbach conjuction doesn't hold in our test");
                break;
            }
        }
        file.close();
    }

    public static boolean isPrime(int n) {
        if (n <= 1) return false;

        for (int i = 2; i <= (int) Math.sqrt(1.0 * n); i++) {
            cnt++;
            if (n % i == 0) return false;
        }
        return true;
    }

    //10 17 3
    public static boolean passGoldConj(int n) {
        //判断某一个数字是否可以由两个素数组成
        boolean flag = false;
        for (int i = 4; i <= n; i += 2) {
            flag = false;
            for (int j = 2; j <= i / 2; j++) {
                if (isPrime(j) && isPrime(i - j)) {
//                    System.out.println("偶数："+i+"  ( "+j+" , "+(i-j)+" )");
                    flag = true;
                    break;
                }
            }
            if (!flag) return false;
        }

        return flag;
    }
}
